
<template>
  <div>
    <el-card>
      <template #header>
        <span>采购订单管理</span>
        <el-button type="primary" style="float: right;" @click="showCreateDialog = true">
          创建订单
        </el-button>
      </template>
      
      <el-table :data="orders" style="width: 100%">
        <el-table-column prop="id" label="订单号" />
        <el-table-column prop="type" label="采购类型" />
        <el-table-column prop="supplier" label="供应商" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" />
      </el-table>
    </el-card>

    <el-dialog v-model="showCreateDialog" title="创建采购订单">
      <el-form :model="newOrder" label-width="80px">
        <el-form-item label="采购类型">
          <el-select v-model="newOrder.type" placeholder="请选择">
            <el-option label="总部集中采购" value="central" />
            <el-option label="区域柔性采购" value="regional" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商">
          <el-input v-model="newOrder.supplier" />
        </el-form-item>
        <el-form-item label="商品">
          <el-input v-model="newOrder.item" placeholder="商品名称" />
        </el-form-item>
        <el-form-item label="数量">
          <el-input-number v-model="newOrder.quantity" :min="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showCreateDialog = false">取消</el-button>
        <el-button type="primary" @click="createOrder">创建</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'

const orders = ref([])
const showCreateDialog = ref(false)
const newOrder = ref({
  type: '',
  supplier: '',
  item: '',
  quantity: 1
})

const loadOrders = async () => {
  const response = await axios.get('/api/procurement/orders')
  orders.value = response.data
}

const createOrder = async () => {
  await axios.post('/api/procurement/orders', newOrder.value)
  showCreateDialog.value = false
  loadOrders()
}

const getStatusType = (status) => {
  const types = {
    '待确认': 'info',
    '已确认': 'success',
    '运输中': 'warning',
    '已收货': 'success'
  }
  return types[status] || 'info'
}

onMounted(() => {
  loadOrders()
})
</script>
